/************************************************************************
                                                                     
   Xceed Ultimate ListBox for Silverlight                                                                                                                                            
   Copyright (C) 2010 Xceed Software Inc.    
                                                                     
   This program is provided to you under the terms of the GNU General Public  
   License version 2 as published by the Free Software Foundation. 
        
   This program is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY, without even the implied warranty of 
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License along 
   with this program, if not, write to the Free Software Foundation, Inc., 
   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

   This program can be provided to you by Xceed Software Inc. under a
   proprietary commercial license agreement for use in non-Open Source
   projects. Visit Xceed.com to find the commercial edition of 
   Xceed Ultimate ListBox for Silverlight.                                    
                                                                      
 **********************************************************************/

using System;

namespace Xceed.Silverlight.Data
{
  internal sealed class DataPathHelper : ParentHelper<DataPath>
  {
    internal DataPathHelper( IDataHelper childHelper )
      : base( childHelper )
    {
    }


    public static DataPath StaticConvertLocal( object value )
    {
      if( value == null )
        throw new ArgumentNullException( "value" );

      DataItem dataItem = value as DataItem;

      if( dataItem == null )
        throw new ArgumentException( "The specified value must be a DataItem instance.", "value" );

      return new DataPath( dataItem );
    }

    public static object StaticConvertBackLocal( DataPath value )
    {
      if( value == null )
        throw new ArgumentNullException( "value" );

      if( value.Depth != 1 )
        throw new InvalidOperationException( "The provided data path is invalid." );

      return value.Path[ 0 ];
    }

    internal override object ConvertLocal( object value )
    {
      return DataPathHelper.StaticConvertLocal( value );
    }

    internal override object ConvertBackLocal( object value )
    {
      DataPath dataPath = value as DataPath;

      if( dataPath != null )
        return DataPathHelper.StaticConvertBackLocal( dataPath );

      return value;
    }
  }
}
